var Upload = {
   feed: null,
   onUpload: null,
   created: false,
   moving: false,
   mimeService: Components.classes["@mozilla.org/mime;1"].getService(Components.interfaces.nsIMIMEService),
   init: function(data) {
      this.feed = data.feed;
      this.onUpload = data.onUpload;
      document.title = data.title;
   },
   actionNext: function() {
      if (this.moving) {
         this.moving = false;
         return true;
      }
      this.moving = true;
      this.type = document.getElementById('action-type').selectedItem.value;
      document.documentElement.advance(this.type);
      return false;
   },
   finish: function() {
      if (this.created) {
         return true;
      }
      if (this.type=="create") {
         var slug = (document.getElementById('slug').value+"").trim();
         var mediaType = (document.getElementById('media-type').value+"").trim();
         var text = document.getElementById('text').value+"";
         if (slug.length==0) {
            alert("You must specify a file name.");
            return false;
         }
         if (mediaType.length==0) {
            alert("You must specify a media type.");
            return false;
         }
         document.getElementById('slug').disabled = true;
         document.getElementById('media-type').disabled = true;
         document.getElementById('text').disabled = true;
         document.documentElement.getButton('back').disabled = true;
         document.documentElement.getButton('finish').disabled = true;
         document.getElementById('create-status').value = "Creating resource...";
         var currentWindow = window;
         var currentApp = this;
         var currentDoc = document;
         var currentLog = LOG;
         this.feed.addResource(text,{
            slug: slug,
            contentType: mediaType,
            onSuccess: function() {
               try {
                  currentApp.created = true;
                  currentApp.onUpload();
               } catch (ex) {
                  logger = Components.classes["@mozilla.org/consoleservice;1"]
                       .getService(Components.interfaces.nsIConsoleService);
                  logger.logStringMessage("Failed to notify upload: "+ex);
               }
               setTimeout(function() { window.close(); },100);
            },
            onFailure: function(status) {
               try {
                  currentDoc.getElementById('slug').disabled = false;
                  currentDoc.getElementById('media-type').disabled = false;
                  currentDoc.getElementById('text').disabled = false;
                  currentDoc.documentElement.getButton('back').disabled = false;
                  currentDoc.documentElement.getButton('finish').disabled = false;
                  currentDoc.getElementById('create-status').value = "Creating failed due to status "+status;
               } catch (ex) {
                  logger = Components.classes["@mozilla.org/consoleservice;1"]
                       .getService(Components.interfaces.nsIConsoleService);
                  logger.logStringMessage("Failed to notify upload: "+ex);
               }
            }
         });
         return false;
      } else {
         return true;
      }
   },
   browseForFiles: function() {
      var nsIFilePicker = Components.interfaces.nsIFilePicker;
      var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
      fp.init(window, "Choose File to Upload", nsIFilePicker.modeOpenMultiple);
      if(fp.show() == nsIFilePicker.returnOK) {
         var files = fp.files;
         var listbox = document.getElementById('files');
         while (files.hasMoreElements()) {
            var file = files.getNext().QueryInterface(Components.interfaces.nsILocalFile);
            var found = null;
            for (var i=0; i<listbox.childNodes.length; i++) {
               if (listbox.childNodes[i].value==file.path) {
                  found = listbox.childNodes[i];
               }
            }
            if (!found) {
               var item = document.createElement("listitem");
               item.setAttribute("value",file.path);
               item.setAttribute("label",file.path);
               listbox.appendChild(item);
            }
         }
      }
   },
   removeSelected: function() {
      var listbox = document.getElementById('files');
      while (listbox.selectedItems.length>0) {
         listbox.removeChild(listbox.selectedItems[0]);
      }
   },
   batchUpload: function() {
      document.getElementById('upload-choice').hidden = true;
      document.getElementById('info').hidden = false;
      this.files = [];
      var listbox = document.getElementById('files');
      for (var i=0; i<listbox.childNodes.length; i++) {
         this.files[this.files.length] = listbox.childNodes[i].value;
      }
      var currentApp = this;
      document.documentElement.getButton('back').disabled = true;
      var doDisplay = function(file,contentType) {
         document.getElementById('info-file').value = "File: "+file.leafName;
         document.getElementById('info-content-type').value = "Media Type: "+contentType;
         return {}
      };
      var onNext = function() {
         if (currentApp.files.length>0) {
            currentApp.doUpload(doDisplay,onNext);
         } else {
            currentApp.onUpload();
            document.getElementById('info').hidden = true;
            document.getElementById('success').hidden = false;
            document.documentElement.getButton('cancel').disabled = true;
         }
      }
      this.doUpload(doDisplay,onNext);
   },
   doUpload: function(doDisplay,onNext) {
      var file = this.files[0];
      var ifile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
      ifile.initWithPath(file);
      this.files.splice(0,1);
      var contentType = "application/octet-stream";
      try {
         contentType  = this.mimeService.getTypeFromFile(ifile);
      } catch (ex) {
         contentType = "application/octet-stream";
      }
      var spec = {};
      if (doDisplay) {
         spec = doDisplay(ifile,contentType);
      }
      var doNext = onNext;
      var request = this.feed.addResourceFromFile(file,{
         slug: spec.slug,
         contentType: spec.contentType,
         onSuccess: function() {
            try {
               setTimeout(function() {doNext()},100);
            } catch (ex) {
               alert(ex);
            }
         },
         onFailure: function(status) {
            alert("Unable to upload file "+file+" , status "+status);
         }
      });
   },
   inspectUpload: function() {
      document.getElementById('upload-choice').hidden = true;
      document.getElementById('inspect').hidden = false;
      document.documentElement.getButton('finish').disabled = true;
      document.documentElement.getButton('back').disabled = true;
      this.files = [];
      var listbox = document.getElementById('files');
      for (var i=0; i<listbox.childNodes.length; i++) {
         this.files[this.files.length] = listbox.childNodes[i].value;
      }
      
      this.showInspect();
   },
   showInspect: function() {
      var file = this.files[0];
      var ifile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
      ifile.initWithPath(file);
      var contentType = this.mimeService.getTypeFromFile(ifile);
      
      document.getElementById('inspect-path').value = ifile.path;
      document.getElementById('inspect-file').value = ifile.leafName;
      document.getElementById('inspect-content-type').value = contentType;
   },
   doInspectUpload: function() {
      var currentApp = this;
      var currentDocument = document;
      this.doUpload(
         function() {
            return {
               slug: currentDocument.getElementById('inspect-file').value,
               contentType: currentDocument.getElementById('inspect-content-type').value
            }
         },
         function() {
            currentApp.created = true;
            if (currentApp.files.length>0) {
               currentApp.showInspect();
            } else {
               currentApp.onUpload();
               document.getElementById('inspect').hidden = true;
               document.getElementById('success').hidden = false;
               document.documentElement.getButton('cancel').disabled = true;
               document.documentElement.getButton('finish').disabled = false;
            }
         }
      );
   },
   doInspectSkip: function() {
      this.files.splice(0,1); 
      if (this.files.length>0) {
         this.showInspect();
      } else {
         if (this.created) {
            this.onUpload();
         }
         document.getElementById('inspect').hidden = true;
         document.getElementById('success').hidden = false;
         document.documentElement.getButton('cancel').disabled = true;
         document.documentElement.getButton('finish').disabled = false;
      }
   }
   
}